<template>
  <div id="user_changePassword">
    <div class="changePassword_tittle">
      <h2>修改密码</h2>
    </div>
    <div class="changePassword_content">
      <el-form :model="form" :rules="rules" ref="formRef" label-width="100px">
        <el-form-item label="原密码：" prop="oldPwd">
          <el-input v-model="form.oldPwd" type="password" show-password></el-input>
        </el-form-item>
        <el-form-item label="新密码：" prop="newPwd">
          <el-input v-model="form.newPwd" type="password" show-password></el-input>
        </el-form-item>
        <el-form-item label="确认密码：" prop="confirmNewPwd">
          <el-input v-model="form.confirmNewPwd" type="password" show-password></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="savePwd">
            <span class="iconfont">&#xe601;</span>
            <span> 修改密码</span>
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { updatePwdService } from '@/api/user'
import { Message } from 'element-ui'
import store from '@/store'
export default {
  data () {
    return {
      form: {
        oldPwd: '',
        newPwd: '',
        confirmNewPwd: ''
      },
      rules: {
        oldPwd: [
          { required: true, message: '请输入原密码', trigger: 'blur' },
          { pattern: /^[/\S]{6,20}$/, message: '长度在 6 到 20 个非空字符', trigger: 'blur' }],
        newPwd: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { pattern: /^[/\S]{6,20}$/, message: '长度在 6 到 20 个非空字符', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (this.form.oldPwd === this.form.newPwd) {
                return callback(new Error('新密码不能和旧密码一致'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }],
        // 确认密码
        confirmNewPwd: [
          { required: true, message: '请输入原密码', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (!value) return callback(new Error('请再次输入新密码'))
              if (this.form.newPwd !== this.form.confirmNewPwd) {
                return callback(new Error('两次密码不一致'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }]
      }
    }
  },
  async created () {
    await this.$store.dispatch('userStore/getBaseInfoService')
  },
  methods: {
    async savePwd () {
      await this.$refs.formRef.validate()
      // TODO1: 调用修改密码接口
      const res = await updatePwdService(this.form.oldPwd, this.form.newPwd)
      res.status === 1 ? await Message.error(res.message) : await Message.success(res.message)
      store.commit('userStore/setUSetInfo', {})
      store.commit('userStore/setBaseInfo', {})
      this.$router.push('/login')
    }

  },
  mounted () {
  }

}
</script>

<style lang="less" scoped>
.changePassword_tittle {
  margin-top: 30px;
  text-align: center;
  color: #000;
}

.changePassword_content {
  padding: 0 80px;

  /deep/ .el-form-item {
    margin: 50px auto;

    /deep/ .el-input__inner {
      border: 1px solid #ccc;
    }

  }

  /deep/ .el-button {
    width: 100%
  }

}
</style>
